package com.blog.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.blog.dao.ArticleMapper;
import com.blog.model.Article;
import com.blog.model.ArticleWithBLOBs;
import com.blog.service.interfaces.IarticleService;

@Service("articleService")
public class articleService implements IarticleService {
	@Autowired
	ArticleMapper articleMapper;
	//@Autowired
	//JedisService jedisService;
	
	
	public  int addArticleTodatabase(ArticleWithBLOBs articleWithBLOBs){
		return articleMapper.insertSelective(articleWithBLOBs);
	}
	
	public  List<ArticleWithBLOBs> getArticleFromdatabase(String page){
		int page0=Integer.valueOf(page);
		//if(page0!=1){page0=page0*5;}
		page0=(page0-1)*6;
		return articleMapper.selectByPage(page0);
	}
	
	public List<ArticleWithBLOBs> getAllArticle(){
		return articleMapper.getAllArticle();
	}
	
	public  ArticleWithBLOBs getArticleById(String id){
		if(id==""){id="1";}
		return articleMapper.selectByPrimaryKey(Integer.valueOf(id));
	}
	
	public int incview(String articleId){
		//return jedisService.inc("article_view"+articleId);
		return 1;
	}
	
	public int getview(String articleId){
		//return jedisService.get("article_view"+articleId);
		return 1;
	}
	
	public Map<String, String> getPageList(String page){
		Map<String,String> map=new LinkedHashMap<String,String>();
		int totalnum=Integer.valueOf(articleMapper.getResultNum());
		int maxPageId=(int)Math.ceil((double)totalnum/6);
		
		
		if(!page.equals("1")){
			map.put("beforePageId",String.valueOf(Integer.valueOf(page)-1));
		}
		if(!page.equals(String.valueOf(maxPageId))){
			map.put("nextPageId",String.valueOf(Integer.valueOf(page)+1));}
		
		//页数小于7
		if(maxPageId<7){
			for(int u=1;u<=maxPageId;u++){map.put("page"+String.valueOf(u),String.valueOf(u));}
			return map;
		}
		
		if(Integer.valueOf(page)+6<=maxPageId){
			//页数大于7且 当前页据尾页大于7页
			map.put("page1",String.valueOf(Integer.valueOf(page)));
			map.put("page2",String.valueOf(Integer.valueOf(page)+1));
			map.put("page3",String.valueOf(Integer.valueOf(page)+2));
			map.put("page4",String.valueOf(Integer.valueOf(page)+3));
			map.put("page5",String.valueOf(Integer.valueOf(page)+4));
			map.put("page6",String.valueOf(Integer.valueOf(page)+5));
			map.put("page7",String.valueOf(Integer.valueOf(page)+6));
		}else{
			map.put("page1",String.valueOf(maxPageId-6));
			map.put("page2",String.valueOf(maxPageId-5));
			map.put("page3",String.valueOf(maxPageId-4));
			map.put("page4",String.valueOf(maxPageId-3));
			map.put("page5",String.valueOf(maxPageId-2));
			map.put("page6",String.valueOf(maxPageId-1));
			map.put("page7",String.valueOf(maxPageId));
		}
		
		return map;
		
	}

	 
	
	public int updateArticleTodatabase(ArticleWithBLOBs articleWithBLOBs) { 
		return articleMapper.updateByPrimaryKeySelective(articleWithBLOBs);
	}

	public int delArticle(String id) {
		return articleMapper.deleteByPrimaryKey(Integer.valueOf(id));
	}

	public int increasePoint(int point, String id) {
		return articleMapper.increasePoint(point,Integer.valueOf(id));
	}

	
	public List<ArticleWithBLOBs> getpopularFromdatabase() {
		return articleMapper.selectBypopular();
	}

	
	public List<ArticleWithBLOBs> getCategoryArticleFromdatabase(String tag) {
		return articleMapper.getCategoryArticle(tag);
	}

	public List<ArticleWithBLOBs> getSearchResultFromdatabase(String key) {
		return articleMapper.getSearchResult("%"+key+"%");
	}
	
	public List<ArticleWithBLOBs> getCategory() {
		return articleMapper.getCategory();
	}

	
	public List<ArticleWithBLOBs> getwaterfall() {
		// TODO Auto-generated method stub
		return articleMapper.getwaterfall();
	}

	@Transactional 
	public void test(){
		ArticleWithBLOBs articleWithBLOBs=new ArticleWithBLOBs();
		articleWithBLOBs.setTitle("e44");
		articleWithBLOBs.setContent("33");

		articleMapper.insert(articleWithBLOBs);
		//throw new RuntimeException();
	}
	
	//读取数据库存的md数量
	public Integer getMdNumFromDB(){
		return articleMapper.getDbMdNum();
	}
	
	//检测以此标题的文章是否存在
	public List<ArticleWithBLOBs> checkExistTitle(String title){
		return articleMapper.checkExistTitle(title);
	}
	
	//返回数据库的md文件列表
	public List<ArticleWithBLOBs> getDbMDList(){
		return articleMapper.getDbMDList();
	}
	
}
